import Util from './util';

let framework = undefined;
let saveStep = 5;

class Modules {
    constructor(fw) {
        framework = fw;
    }
    async isNeedRecord(path, type) {
        //检查path对应模块是否需要采集日志
        let module = Util.find(vue.$store.state.modules, item => {
            return item.moduleUrl.indexOf(path) != -1;
        })
        if (module) {
            let user = vue.$f7.data.currentUser;
            let tmp = null;
            if (type == 'in') { //进入
                tmp = {
                    moduleId: module.moduleId,
                    userId: user.userId,
                    userName: user.userName,
                    path: path,
                    startTime: new Date(),
                    endTime: null,
                    skimTime: null
                }
                this.setModuleLog(tmp);
            } else { //退出
                tmp = await this.getModuleLog(path, user.userId); //获取path对应的日志
                if (tmp) {
                    let now = new Date();
                    tmp.skimTime = (new Date().getTime() - tmp.startTime.getTime()) / 1000;
                    tmp.endTime = now;
                    this.setModuleLog(tmp);
                }
                //获取当前用户的功能模块使用日志缓存数据，超过saveStep条则保存至后台
                let tmpModules = await framework.db.modulelog.filter(function (module) {
                    return module.userId == user.userId;
                }).sortBy('startTime')
                //排除最后条数据，因该数据为刚进入页面的日志
                tmpModules.splice(tmpModules.length - 1, 1)
                if (tmpModules.length > saveStep) {
                    this.saveModuleLog(tmpModules);
                }
            }
        }
    }
    setModuleLog(module) {
        framework.db.modulelog.put(module);
    }
    async getModuleLog(path, userId) {
        let modules = await framework.db.modulelog.filter(function (module) {
            return module.userId == userId && module.path == path;
        }).sortBy('startTime');
        return modules ? modules[modules.length - 1] : null;
    }
    saveModuleLog(modules) {
        if (Util.isArray(modules)) {
            let postData = [...modules];
            Util.forEach(modules, (item, i) => {
                if (!item.endTime || item.skimTime < 1) { //排除异常关闭及浏览时间小于1秒数据
                    postData.splice(postData.indexOf(item), 1);
                } else {
                    item.startTime = vue.$framework.formatter.formatDateTime(item.startTime)
                    item.endTime = vue.$framework.formatter.formatDateTime(item.endTime)
                }
            })
            vue.$framework.$f7.request.postJSON('/api/app/mobileModuleLog/saveModuleLogs', {
                logs: postData
            }, r => {
                if (r.success) {
                    //删除缓存
                    Util.forEach(modules, (item, i) => {
                        framework.db.modulelog.delete(item.id)
                    })
                }
                console.log(JSON.stringify(r))
            }, r => {
                console.log(JSON.stringify(r))
            })
        }
    }
}


export default Modules;